using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace QFramework.Example
{
    /// <summary>
    /// 全局数据配置
    /// </summary>
    public class Global : Architecture<Global>
    {
        /// <summary>
        /// 经验值
        /// </summary>
        public static BindableProperty<int> Exp = new BindableProperty<int>(0);

        /// <summary>
        /// 玩家金币
        /// </summary>
        public static BindableProperty<int> Coin = new BindableProperty<int>(0);
        public static BindableProperty<string> CoinStr = new BindableProperty<string>("coin");

        /// <summary>
        /// 玩家等级
        /// </summary>
        public static BindableProperty<int> Level = new BindableProperty<int>(1);

        /// <summary>
        /// 简单能力的攻击力
        /// </summary>
        public static BindableProperty<float> SimpleAbilityAtk = new BindableProperty<float>(1);

        /// <summary>
        /// 简单能力的攻击间隔
        /// </summary>
        public static BindableProperty<float> SimpleAbilityDuration = new BindableProperty<float>(1.5f);

        /// <summary>
        /// 当前游戏运行时间
        /// </summary>
        public static BindableProperty<float> CurrentTime = new BindableProperty<float>(0);

        /// <summary>
        /// 场景中敌人数量
        /// </summary>
        public static BindableProperty<int> EnemyCount = new BindableProperty<int>(0);

        /// <summary>
        /// 怪物波次
        /// </summary>
        public static BindableProperty<int> EnemyWaveCount = new BindableProperty<int>(0);

        /// <summary>
        /// 玩家生命
        /// </summary>
        public static BindableProperty<int> PlayerHp = new BindableProperty<int>(3);

        /// <summary>
        /// 玩家最大生命值
        /// </summary>
        public static BindableProperty<int> PlayerMaxHp = new BindableProperty<int>(3);
        public static BindableProperty<string> PlayerMaxHpStr = new BindableProperty<string>("PlayerMaxHp");

        /// <summary>
        /// 经验 和 金币 掉落概率
        /// </summary>
        public static BindableProperty<float> ExpDropPrecent = new BindableProperty<float>(0.3f);
        public static BindableProperty<float> CoinDropPrecent = new BindableProperty<float>(0.1f);
        public static BindableProperty<float> HpDropPrecent = new BindableProperty<float>(0.1f);
        public static BindableProperty<float> BombDropPrecent = new BindableProperty<float>(0.1f);
        public static BindableProperty<float> AllExpDropPrecent = new BindableProperty<float>(0.1f);
        public static BindableProperty<string> ExpDropPrecentStr = new BindableProperty<string>("ExpDropPrecent");
        public static BindableProperty<string> CoinDropPrecentStr = new BindableProperty<string>("CoinDropPrecent");
        public static BindableProperty<string> HpDropPrecentStr = new BindableProperty<string>("HpDropPrecent");
        public static BindableProperty<string> BombDropPrecentStr = new BindableProperty<string>("BombDropPrecent");
        public static BindableProperty<string> AllExpDropPrecentStr = new BindableProperty<string>("AllExpDropPrecent");


        [RuntimeInitializeOnLoadMethod]
        public static void AutoInit()
        {
            ResKit.Init();
            UIKit.Root.SetResolution(1920, 1080, 1);

            PlayerMaxHp.Value = PlayerPrefs.GetInt(PlayerMaxHpStr.Value, 3);
            Coin.Value = PlayerPrefs.GetInt(CoinStr.Value, 0);
            ExpDropPrecent.Value = PlayerPrefs.GetFloat(ExpDropPrecentStr.Value, 0.3f);
            CoinDropPrecent.Value = PlayerPrefs.GetFloat(CoinDropPrecentStr.Value, 0.1f);
            HpDropPrecent.Value = PlayerPrefs.GetFloat(HpDropPrecentStr.Value, 0.1f);
            BombDropPrecent.Value = PlayerPrefs.GetFloat(BombDropPrecentStr.Value, 0.1f);
            AllExpDropPrecent.Value = PlayerPrefs.GetFloat(AllExpDropPrecentStr.Value, 0.1f);

            PlayerHp.Value = PlayerMaxHp.Value;

            PlayerMaxHp.Register(maxHp =>
            {
                PlayerPrefs.SetInt(PlayerMaxHpStr.Value, maxHp);
            });

            Coin.Register(coin =>
            {
                PlayerPrefs.SetInt(CoinStr.Value, coin);
            });

            ExpDropPrecent.Register(expDropPrecent =>
            {
                PlayerPrefs.SetFloat(ExpDropPrecentStr.Value, expDropPrecent);
            });

            CoinDropPrecent.Register(coinDropPrecent =>
            {
                PlayerPrefs.SetFloat(CoinDropPrecentStr.Value, coinDropPrecent);
            });

            HpDropPrecent.Register(hpDropPrecent =>
            {
                PlayerPrefs.SetFloat(HpDropPrecentStr.Value, hpDropPrecent);
            });

            BombDropPrecent.Register(bombDropPrecent =>
            {
                PlayerPrefs.SetFloat(BombDropPrecentStr.Value, bombDropPrecent);
            });

            AllExpDropPrecent.Register(allExpDropPrecent =>
            {
                PlayerPrefs.SetFloat(AllExpDropPrecentStr.Value, allExpDropPrecent);
            });

            //PlayerPrefs.Save();
        }

        /// <summary>
        /// 数据初始化
        /// </summary>
        public static void OnInitData()
        {
            Time.timeScale = 1;
            PlayerHp.Value = PlayerMaxHp.Value;
            Exp.Value = 0;
            Level.Value = 1;
            SimpleAbilityAtk.Value = 1;
            CurrentTime.Value = 0;
            EnemyCount.Value = 0;
            SimpleAbilityDuration.Value = 1.5f;
            EnemyWaveCount.Value = 0;
        }

        /// <summary>
        /// 经验升级公式
        /// </summary>
        public static int ExpToNextLevel()
        {
            return Level.Value * 5;
        }



        public static void OnDropObject(GameObject targetObj)
        {
            if (targetObj == null) return;
            float precent = Random.Range(0f, 1f);
            if (precent <= ExpDropPrecent.Value)
            {
                DropObjectManager.Default.Exp.Instantiate()
                   .Position(targetObj.transform.position)
                   .Show();

                return;
            }

            precent = Random.Range(0f, 1f);
            if (precent <= CoinDropPrecent.Value)
            {
                DropObjectManager.Default.Coin.Instantiate()
                   .Position(targetObj.transform.position)
                   .Show();

                return;
            }

            precent = Random.Range(0f, 1f);
            if (precent <= HpDropPrecent.Value)
            {
                DropObjectManager.Default.Hp.Instantiate()
                   .Position(targetObj.transform.position)
                   .Show();

                return;
            }

            precent = Random.Range(0f, 1f);
            if (precent <= BombDropPrecent.Value)
            {
                DropObjectManager.Default.Bomb.Instantiate()
                   .Position(targetObj.transform.position)
                   .Show();

                return;
            }

            precent = Random.Range(0f, 1f);
            if (precent <= AllExpDropPrecent.Value)
            {
                DropObjectManager.Default.AllExp.Instantiate()
                   .Position(targetObj.transform.position)
                   .Show();

                return;
            }
        }

        protected override void Init()
        {
            //注册模块的操作
            //XXX Model

            this.RegisterSystem(new CoinUpgradeSystem());
            this.RegisterSystem(new SaveSystem());
            this.RegisterSystem(new ExpUpgradeSystem());
        }
    }
}
